package me.dqn.noway.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.dqn.noway.R;
import me.dqn.noway.bean.PasswordInformation;
import me.dqn.noway.precenter.MyPresenter;

import static android.content.ContentValues.TAG;

public class MainRecycleViewAdapter extends RecyclerView.Adapter<MainRecycleViewAdapter.MyViewHolder> {
    public List<PasswordInformation> passwordInformations;
    private Context context;
    private MyPresenter presenter;
    public MainRecycleViewAdapter(Context context, MyPresenter presenter) {
        this.passwordInformations = new ArrayList<>();
        this.presenter = presenter;
        this.context = context;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.main_card, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        PasswordInformation passwordInformation = passwordInformations.get(position);
        holder.username.setText(passwordInformation.getUsername());
        holder.type.setText(passwordInformation.getType());
        holder.detail.setText(passwordInformation.getDetail());
        holder.password.setText(passwordInformation.getPassword());
        holder.mail.setText(passwordInformation.getMail());
        Animation animationStart = AnimationUtils.loadAnimation(context, R.anim.item_in);
        holder.cardView.startAnimation(animationStart);
        holder.cardView.setOnLongClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(R.string.SURE_DELETE);
            builder.setMessage(R.string.DELETE_MESSAGE);
            builder.setPositiveButton(R.string.sure, (dialog, which) -> {
                presenter.deleteInformation(passwordInformation.getId());
            });
            builder.setNegativeButton(R.string.cancel, (dialog, which) -> {
                presenter.toastMessages(context.getString(R.string.CANCEL_DELETE), Toast.LENGTH_SHORT);
            });
            builder.show();
            return false;
        });
    }
    @Override
    public int getItemCount() {
        return passwordInformations.size();
    }
    /**
     * view holder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView username;
        TextView password;
        TextView mail;
        TextView detail;
        TextView type;
        CardView cardView;
        public MyViewHolder(View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.id_card);
            username = itemView.findViewById(R.id.id_detail_name);
            password = itemView.findViewById(R.id.id_detail_password);
            mail = itemView.findViewById(R.id.id_detail_mail);
            detail = itemView.findViewById(R.id.id_detail_detail);
            type = itemView.findViewById(R.id.id_detail_type);
        }
    }
}
